﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using HaiHui.Business;
using System.ComponentModel;

namespace HaiHui.Data
{
    /// <summary>
    /// Dupa vreo 2 refactoring-uri masive care aproape ca m-au lasat fara proiect, 
    /// am ajuns victorios la acest "DataStore" de tot cacatul.
    /// </summary>
    class DataStore
    {
        /// <summary>
        /// Muncitorul static, bun baiat.
        /// </summary>
        public static BackgroundWorker Worker = new BackgroundWorker();
        public static LoadItemsWindow LoadWindow = new LoadItemsWindow();

        public static event EventHandler ShowMainWindow;

        static DataStore()
        {
            Worker.WorkerReportsProgress = true;
            Worker.DoWork += new DoWorkEventHandler(bgWorker_DoWork);
            Worker.ProgressChanged += new ProgressChangedEventHandler(bgWorker_ProgressChanged);
            Worker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(Worker_RunWorkerCompleted);
        }


        private static List<Album> m_Albums = new List<Album>();

        /// <summary>
        /// Albumele, toate.
        /// </summary>
        public static List<Album> Albums
        {
            get { return m_Albums; }
        }

        /// <summary>
        /// Apelata de BGWorker
        /// </summary>
        private static void LoadAlbumUpdatesInternal()
        {
            List<Album> newAlbums = HttpUtils.GetAlbumList();
            if (newAlbums.Count > 0)
            {
                m_Albums.AddRange(newAlbums.ToArray());
            }
        }

        /// <summary>
        /// De aici dam drumul incarcarii
        /// </summary>
        public static void LoadAlbumUpdates()
        {
            LoadWindow.Show();

            Worker.RunWorkerAsync();
        }


        /// <summary>
        /// Hm, oare ce'o sa faca asta?... 
        /// </summary>
        /// <param name="idAlbum"></param>
        public static void LoadPictureUpdates(int idAlbum)
        {

        }

        static void bgWorker_ProgressChanged(object sender, ProgressChangedEventArgs e)
        {
            if (e.UserState is Picture)
            {
                (e.UserState as Picture).GetImages();
            }
            else
                LoadWindow.DisplayProgress(e.UserState.ToString());
        }

        static void bgWorker_DoWork(object sender, DoWorkEventArgs e)
        {
            ///Dam drumul la incarcarea albumelor
            DataStore.LoadAlbumUpdatesInternal();



            ///Vorbe intelepte: Fiindca se petrec pe alte thread-uri (cate unul pe album), 
            ///incarcarea pozelor nu este totuna cu incarcarea albumelor.
            ///
            ///Prin urmare, dupa ce luam albumele, tre sa asteptam si pozele.
            bool PictureDownloadCompleted;

            do
            {
                PictureDownloadCompleted = true;
                foreach (Album album in DataStore.Albums)
                {
                    if (!album.PicturesLoaded)
                    {
                        PictureDownloadCompleted = false;
                        break;
                    }
                }
            } while (!PictureDownloadCompleted);
        }

        /// <summary>
        /// Ascundem forma de incarcare si tragem  un pârţ  pardon event ca sa aratam forma principala.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        static void Worker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            LoadWindow.Visibility = System.Windows.Visibility.Collapsed;

            if (ShowMainWindow != null)
                ShowMainWindow(null, null);
        }
    }
}
